<template>
    <div>
        <el-card>
            <!-- 为ECharts准备一个具备大小（宽高）的Dom -->
            <div id="main" style="width: 600px;height:400px;"></div>
        </el-card>
    </div>
</template>

<script>
    import echarts from 'echarts'

    export default {
        data() {
            return {}
        },
        created() {
        },
        mounted() {
            this.charts()
        },
        methods: {
            charts() {
                axios.get("http://localhost:8181/report/findAll").then(function (resp) {
                    let results = resp.data

                    var courses = {
                        "1309": "MySQL优化",
                        "1336": "Crontab",
                        "2402": "Swift",
                        "2801": "SpringData",
                        "3078": "R",
                        "3369": "机器学习",
                        "4018": "redis",
                        "12552": "神经网络",
                        "13098": "Docker",
                        "11239": "Python"
                    }


                    var legend_data = []
                    var series_data = []
                    for (var i = 0; i < results.length; i++) {
                        legend_data.push(courses[results[i].cms_id])
                        series_data.push({
                            value: results[i].times,
                            name: courses[results[i].cms_id]
                        })
                    }

                    // 基于准备好的dom，初始化echarts实例
                    var myChart = echarts.init(document.getElementById('main'));
                    // 指定图表的配置项和数据
                    var option = {
                        title: {
                            text: '最受欢迎的TopN课程',
                            left: 'center'
                        },
                        tooltip: {
                            trigger: 'item',
                            formatter: '{a} <br/>{b}: {c} ({d}%)'
                        },
                        legend: {
                            orient: 'vertical',
                            left: 'left',
                            data: legend_data
                        },
                        series: [
                            {
                                name: '课程',
                                type: 'pie',
                                radius: '55%',
                                center: ['50%', '60%'],
                                emphasis: {
                                    itemStyle: {
                                        shadowBlur: 10,
                                        shadowOffsetX: 0,
                                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                                    }
                                },
                                data: series_data
                            }
                        ]
                    };

                    // 使用刚指定的配置项和数据显示图表。
                    myChart.setOption(option);
                })
            }
        }
    }
</script>

<style scoped>

</style>